
import bpy

from 全局控制.Node.数据编辑.物体 import bl_修改器公共选项

from 节点.全局控制.Node.物体数据生成 import bl_集合物体处理
from 节点.全局控制.Node.节点和插座基类 import bl_节点基类
from 节点.Core import bl_UI配色
from 节点.Core import bl_Mesh

import engine.initLIB.load_dll as engineLib
import ctypes


engineLib.flexCAPI.f_创建网格分裂修改器节点.restype = ctypes.c_bool
engineLib.flexCAPI.f_创建网格变换节点.restype = ctypes.c_bool
engineLib.flexCAPI.f_创建从网格创建物体.restype = ctypes.c_bool
engineLib.flexCAPI.f_创建从网格构建体素节点.restype = ctypes.c_bool







E_网格拆分方式类型枚举 = [
	("TYPE_逐面", "逐面", "", 0),
	("TYPE_范围", "范围", "", 1),
]


def f_update_网格拆分方式类型枚举(self, context):
	data = list(str(self.m_网格拆分方式类型))
	engineLib.flexCAPI.f_node_传入BL数据(ctypes.c_wchar_p(self.id_data.name), ctypes.c_wchar_p(self.name), ctypes.py_object(data));
	
	if context :
		self.id_data.f_节点数据更新_更新树([self])



class C_Node_网格拆分修改器(bl_节点基类.C_Node_基类):
	bl_dname = 'bl_node_网格拆分修改器'
	bl_label = '网格拆分修改器'
	
	m_网格拆分方式类型 = bpy.props.EnumProperty(items=E_网格拆分方式类型枚举, name="类型", default="TYPE_逐面", update=f_update_网格拆分方式类型枚举)
	def init(self, context):
		super(C_Node_网格拆分修改器, self).f_init(context)
		self.inputs.new('C_Socket_网格_Array1D', name='网格')
		self.outputs.new('C_Socket_网格_Array1D', name='新网格')
		
		self.f_toCPP(self.id_data);

	def draw_buttons(self, context, layout):
		layout.prop(self, 'm_网格拆分方式类型')
		
	def f_toCPP(self, tree):
		是否更新 = engineLib.flexCAPI.f_创建网格分裂修改器节点(ctypes.c_wchar_p(tree.name), ctypes.c_wchar_p(self.name), self.f_getPyObject(tree));
		if 是否更新 :
			self.use_custom_color = True
			self.color = bl_UI配色.g_网格类节点颜色


class C_NODE_MENU_网格拆分修改器(bpy.types.Operator):
	bl_idname = 'node.global_node_obj_modifiers_mesh_split'
	bl_label = '网格拆分修改器'
	bl_options = {'REGISTER', 'UNDO'}
	def execute(operator, context):
		bpy.ops.node.add_node(type='C_Node_网格拆分修改器', use_transform=True)
		return {'FINISHED'}


















class C_Node_网格变换(bl_节点基类.C_Node_基类):
	bl_dname = 'bl_node_网格变换'
	bl_label = '网格变换'
	
	def init(self, context):
		super(C_Node_网格变换, self).f_init(context)
		self.inputs.new('C_Socket_网格_Array1D', name='网格')
		self.inputs.new('C_Socket_矩阵4X4_Array1D', name='矩阵')
		self.inputs.new('C_Socket_I32_Array1D', name="A索引")
		self.inputs.new('C_Socket_I32_Array1D', name="B索引")

		self.outputs.new('C_Socket_网格_Array1D', name='网格')
		
		self.f_toCPP(self.id_data);

	def update(self):
		aid = self.inputs.find('A索引')
		bid = self.inputs.find('B索引')
		#print('aid = ', aid, '  bid = ', bid)
		if aid >= 0 and bid >= 0 :
			if self.inputs[aid].is_linked :
				self.inputs[aid].hide = False
				self.inputs[bid].hide = True
			elif self.inputs[bid].is_linked :
				self.inputs[aid].hide = True
				self.inputs[bid].hide = False
			else:
				self.inputs[aid].hide = False
				self.inputs[bid].hide = False
			
		
	def f_toCPP(self, tree):
		是否更新 = engineLib.flexCAPI.f_创建网格变换节点(ctypes.c_wchar_p(tree.name), ctypes.c_wchar_p(self.name), self.f_getPyObject(tree));
		if 是否更新 :
			self.inputs['A索引'].f_update()
			self.inputs['B索引'].f_update()

			self.use_custom_color = True
			self.color = bl_UI配色.g_网格类节点颜色


class C_NODE_MENU_网格变换(bpy.types.Operator):
	bl_idname = 'node.global_node_meshes_tranform'
	bl_label = '网格变换'
	bl_options = {'REGISTER', 'UNDO'}
	def execute(operator, context):
		bpy.ops.node.add_node(type='C_Node_网格变换', use_transform=True)
		return {'FINISHED'}































class C_Node_从网格创建新网格(bl_节点基类.C_Node_基类):
	bl_dname = 'bl_node_从网格创建新网格'
	bl_label = '从网格创建新网格'
	
	#m_名称 = bpy.props.StringProperty(update=f_update_创建网格参数)
	def init(self, context):
		super(C_Node_从网格创建新网格, self).f_init(context)
		self.inputs.new('C_Socket_网格', name='网格')
		self.inputs.new('C_Socket_顶点索引', name='面索引')
		self.outputs.new('C_Socket_网格_Array1D', name='新网格')
		self.f_toCPP(self.id_data)
		
		
	def f_toCPP(self, tree):
		是否更新 = engineLib.flexCAPI.f_创建从网格生成新网格节点(ctypes.c_wchar_p(tree.name), ctypes.c_wchar_p(self.name), self.f_getPyObject(tree));
		if 是否更新 :
			pass
		else:
			self.use_custom_color = True
			self.color = bl_UI配色.g_网格类节点颜色
	
class C_NODE_MENU_从网格创建新网格(bpy.types.Operator):
	bl_idname = 'node.from_mesh_create_mesh'
	bl_label = '从网格创建新网格'
	bl_options = {'REGISTER', 'UNDO'}
	def execute(operator, context):
		bpy.ops.node.add_node(type='C_Node_从网格创建新网格', use_transform=True)
		return {'FINISHED'}


















class C_Node_从网格构建体素(bl_节点基类.C_Node_基类):
	bl_dname = 'bl_node_网格体素'
	bl_label = '网格体素'
	
	#m_名称 = bpy.props.StringProperty(update=f_update_创建网格参数)
	def init(self, context):
		super(C_Node_从网格构建体素, self).f_init(context)
		self.inputs.new('C_Socket_网格', name='网格')
		self.inputs.new('C_Socket_F32', name='间距')
		self.inputs.new('C_Socket_Vec3', name='位置')
		self.inputs.new('C_Socket_Vec4', name='旋转')
		self.inputs.new('C_Socket_Vec3', name='缩放')

		self.outputs.new('C_Socket_uVec3', name='维度')
		self.outputs.new('C_Socket_Vec3_Array1D', name='坐标')
		self.outputs.new('C_Socket_I8_Array1D', name='遮罩')
		self.f_toCPP(self.id_data)
		
		self.inputs['间距'].f_setData(0.2)
		self.inputs['旋转'].f_setData((0.0,0.0,0.0, 1.0))
		self.inputs['缩放'].f_setData((1.0,1.0,1.0))
		
	def f_toCPP(self, tree):
		是否更新 = engineLib.flexCAPI.f_创建从网格构建体素节点(ctypes.c_wchar_p(tree.name), ctypes.c_wchar_p(self.name), self.f_getPyObject(tree));
		if 是否更新 :
			for s in self.inputs:
				s.f_update()
		else:
			self.use_custom_color = True
			self.color = bl_UI配色.g_网格类节点颜色
			

class C_NODE_MENU_从网格构建体素(bpy.types.Operator):
	bl_idname = 'node.from_mesh_voxels'
	bl_label = '网格体素'
	bl_options = {'REGISTER', 'UNDO'}
	def execute(operator, context):
		bpy.ops.node.add_node(type='C_Node_从网格构建体素', use_transform=True)
		return {'FINISHED'}















classes = (
	C_Node_网格拆分修改器,
	C_NODE_MENU_网格拆分修改器,

	C_Node_网格变换,
	C_NODE_MENU_网格变换,


	

	C_Node_从网格创建新网格,
	C_NODE_MENU_从网格创建新网格,

	C_Node_从网格构建体素,
	C_NODE_MENU_从网格构建体素,

)

def register():
	for c in classes:
		bpy.utils.register_class(c)
		
def unregister():
	for c in classes:
		bpy.utils.unregister_class(c)





